Ext.define('App.crm.contractsForm', {
    extend: 'Ext.window.Window',
    alias: 'widget.contractsForm',
    initComponent: function () {
        var form = Ext.widget('form', {
            frame: false,
            border: true,
            split: true,
            autoScroll: true,
            bodyPadding: 5,
            bodyBorder: true,
            fieldDefaults: {
                labelWidth: 85,
                labelAlign: 'right',
                msgTarget: 'side'
            },
            defaults: {
                anchor: '100%'
            },
            items: [
                {xtype: 'hidden',
                    name: 'id',
                    dataIndex: 'id'
                },
                {xtype: 'hidden',
                    name: 'deleteFlag',
                    dataIndex: 'deleteFlag'
                },
              
                {
                    xtype: 'fieldset',
                    title: '客户信息',
                    collapsible: true,
                    defaults: {
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [{
                            	 xtype: 'textfield',
                                name: 'number',
                                dataIndex: 'number',
                                fieldLabel: '合同编号',
                                allowBlank: false
                            },
                            {
                           	 xtype: 'textfield',
                               name: 'name',
                               dataIndex: 'name',
                               fieldLabel: '合同名称',
                               allowBlank: false
                           }]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [  new dateTime({
                                name: 'startDate',
                                dataIndex: 'startDate',
                                format: 'Y-m-d',
                                fieldLabel: '生效日期',
                                allowBlank: false
                            }),
                            new dateTime({
                                name: 'endDate',
                                dataIndex: 'endDate',
                                format: 'Y-m-d',
                                fieldLabel: '到期日期',
                                allowBlank: false
                            })]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [ {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                             items: [ {
                            	xtype: 'hidden',
                                name: 'customerId',
                                dataIndex: 'customerId'
                            },{xtype:'textfield',
                                name: 'customerName',
                                dataIndex: 'customerName',
                                fieldLabel: '客户名称',
                                readOnly:true,
                                emailText:'请选择客户',
                                width: 230,
                                allowBlank: false
                            },{
                            	xtype:'button',
                                name: 'btnCustomersId',
                                dataIndex: 'btnCustomersId',
                                iconCls: 'searchIcon',
                                 scope: this,
                                handler: this.onCustomerSel,
                                width: 25
                            }]},
                            {
                                xtype: 'container',
                                layout: 'hbox',
                                margin: '0 0 5 0',
                                defaults: {
                                }, items: [ {xtype:'hidden',
                                    name: 'businessOpportunitiesId',
                                    dataIndex: 'businessOpportunitiesId'
                                },{xtype:'textfield',
                                    name: 'businessOpportunitiesName',
                                    dataIndex: 'businessOpportunitiesName',
                                    fieldLabel: '业务机会',
                                    readOnly:true,
                                    emailText:'请选择业务机会',
                                    width: 230,
                                    allowBlank: false
                                },{
                                	xtype:'button',
                                    name: 'btnBusinessOpportunitiesId',
                                    dataIndex: 'btnBusinessOpportunitiesId',
                                    iconCls: 'searchIcon',
                                scope: this,
                                    handler:this.onBusinessOpportunitiesSel,
                                    width: 25
                                }]}]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [ {xtype:'numberfield',
                                name: 'contractAmount',
                                dataIndex: 'contractAmount',
                                allowDecimals: true,
                                decimalPrecision: 2,
                                mainValue:0,
                                fieldLabel: '合同金额',
                                allowBlank: true
                            }, new comboBox({
                                name: 'status',
                                dataIndex: 'status',
                                fieldLabel: '状态',
                                labelAlign: 'right',
                                codeName: '合同状态',
                                queryMode: 'local',
                                typeAhead: false,
                                forceSelection: true,
                                loadingText: '正在搜索...',
                                allowBlank: false,
                                hideTrigger: false
                            })]}
                    ]},
                {
                    xtype: 'fieldset',
                    title: '地址信息',
                    collapsible: true,
                    defaults: {
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [  {xtype:'textfield',
                        name: 'billingZipCode',
                        dataIndex: 'billingZipCode',
                        fieldLabel: '开单邮编',
                        allowBlank: true
                    },
                    {xtype:'textfield',
                        name: 'billingAddress',
                        dataIndex: 'billingAddress',
                        fieldLabel: '开单地址',
                        allowBlank: true
                    }, {xtype:'textfield',
                        name: 'shipZipCode',
                        dataIndex: 'shipZipCode',
                        fieldLabel: '发货邮编',
                        allowBlank: true
                    }, {xtype:'textfield',
                        name: 'shipAddress',
                        dataIndex: 'shipAddress',
                        fieldLabel: '发货地址',
                        allowBlank: true
                    }]},
                {
                    xtype: 'fieldset',
                    title: '备注信息',
                    collapsible: true,
                    defaults: {
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [ {xtype:'textarea',
                        name: 'specialItem',
                        dataIndex: 'specialItem',
                        fieldLabel: '特殊条款',
                        allowBlank: true
                    },  {xtype:'textarea',
                        name: 'remark',
                        dataIndex: 'remark',
                        fieldLabel: '备注',
                        allowBlank: true
                    }]},
                {
                    xtype: 'fieldset',
                    title: '签名信息',
                    collapsible: true,
                    defaults: {
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [ {xtype:'textfield',
                                name: 'customerSignatory',
                                dataIndex: 'customerSignatory',
                                fieldLabel: '客户签字人',
                                allowBlank: true
                            }   , {xtype:'textfield',
                                name: 'customerSignatoryDuties',
                                dataIndex: 'customerSignatoryDuties',
                                fieldLabel: '签字人职务',
                                allowBlank: true
                            }]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {

                            }, items: [ new dateTime({
                                name: 'customersSigningDate',
                                dataIndex: 'customersSigningDate',
                                format: 'Y-m-d',
                                fieldLabel: '客户签约日期',
                                allowBlank: true
                            })]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1
                            }, items: [ {xtype:'textfield',
                                name: 'companySignatory',
                                dataIndex: 'companySignatory',
                                fieldLabel: '公司签字人',
                                allowBlank: true
                            }, new dateTime({
                                name: 'companySignatureDate',
                                dataIndex: 'companySignatureDate',
                                format: 'Y-m-d',
                                fieldLabel: '公司签字日期',
                                allowBlank: true
                            })]}
                    ]} 
            ]
        });
        Ext.apply(this, {
            title: '新增',
            layout: 'fit',
            height: 500,
            width: 600,
            modal: true,
            constrain: true,
            closeAction: 'hide',
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        //this.up(‘window’)获取了顶级的window组件。接着使用down()方法获取了window的子组件form组件，最后使用getForm()来获取整个form。
                        var form = this.up('contractsForm').down('form');
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/contractsController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    this.up('contractsForm').close();
                                    Ext.getCmp('contractsGrid').getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    }
                },
                {
                    text: "关闭",
                    handler: function () {
                        this.up('contractsForm').close();
                    }
                }
            ],
            items: form
        });

        this.callParent(arguments);
    },
    businessOpportunitiesWin:null,
    onBusinessOpportunitiesSel: function () {
               if( this.down('form').getForm().findField("customerId").getValue()==""){
                   Ext.Msg.show('请选择客户');
                   return;
               }
            if(!this.businessOpportunitiesWin){
                this.businessOpportunitiesWin= new businessOpportunitiesSel({customersId:this.down('form').getForm().findField("customerId").getValue()});
                this.businessOpportunitiesWin.on('onSelectItem', function(data) {
                	this.down('form').getForm().findField('businessOpportunitiesId').setValue(data.id);
                	this.down('form').getForm().findField('businessOpportunitiesName').setValue(data.bussName);
                }, this);
            }
        this.businessOpportunitiesWin.show();
    } ,customerSelWin:null,
    onCustomerSel:function(){
        if(!this.customerSelWin){
            this.customerSelWin= new customersSel();
            this.customerSelWin.on('onSelectItem', function(data) {
                this.down('form').getForm().findField("customerId").setValue(data.id);
                this.down('form').getForm().findField("customerName").setValue(data.company);
            }, this);

        }
        this.customerSelWin.show();
    }
});